package tracer

import (
	baseLog "gitee.com/zaiqiang231/go-base-app/base_app/log"
	"github.com/openzipkin/zipkin-go"
	"github.com/openzipkin/zipkin-go/model"
	"github.com/openzipkin/zipkin-go/reporter"
	zipkinhttp "github.com/openzipkin/zipkin-go/reporter/http"
	"sync"
)

var (
	reporterG  reporter.Reporter
	tracerG    *zipkin.Tracer
	onceTracer sync.Once
)

func InitTracer(url string, serviceName string, hostPort string) (tmpTracer *zipkin.Tracer, err error) {
	onceTracer.Do(func() {
		reporterG = zipkinhttp.NewReporter(url)
		var endpoint *model.Endpoint
		endpoint, err = zipkin.NewEndpoint(serviceName, hostPort)
		if err != nil {
			return
		}
		tmpTracer, err = zipkin.NewTracer(reporterG, zipkin.WithLocalEndpoint(endpoint))
		if err != nil {
			return
		}
		tracerG = tmpTracer
	})
	return
}

func GetTracer() *zipkin.Tracer {
	if tracerG == nil {
		baseLog.Errorf("tracer is %v", tracerG)
	}
	return tracerG
}
